//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.huashi.otg.sdk;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class HsOTGSDK {
	UsbDeviceConnection mDeviceConnection;
	UsbEndpoint epOut;
	UsbEndpoint epIn;
	Context instance;

	public HsOTGSDK(Context activity) throws Exception {
		this.instance = activity;
		int ret = this.initUSB(activity);
		if (ret != 1) {
			Exception e = new Exception();
			if (ret == -1) {
				e.initCause(new Exception());
			} else {
				e.initCause((Throwable) null);
			}

			throw e;
		}
	}

	int initUSB(Context instance) {
		UsbDevice mUsbDevice = null;
		UsbManager manager = (UsbManager) instance.getSystemService(Context.USB_SERVICE);
		if (manager == null) {
			return -99;
		} else {
			/* 获取USB设备列表 */
			HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
			Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
			ArrayList<String> USBDeviceList = new ArrayList<String>();

			while (deviceIterator.hasNext()) {
				UsbDevice ret = (UsbDevice) deviceIterator.next();
				USBDeviceList.add(String.valueOf(ret.getVendorId()));
				USBDeviceList.add(String.valueOf(ret.getProductId()));
				/* 获取特定的设备 vendorId(产商ID)=1024 productId(产品ID)=50010 */
				if (ret.getVendorId() == 1024 && ret.getProductId() == 50010) {
					mUsbDevice = ret;
				}
			}

			int ret1 = this.findIntfAndEpt(manager, mUsbDevice);
			return ret1;
		}
	}

	private int findIntfAndEpt(final UsbManager manager, final UsbDevice mUsbDevice) {
		UsbInterface mInterface = null;
		if (mUsbDevice == null) {
			return -99;
		} else {
			byte connection = 0;
			UsbInterface connection1;
			if (connection < mUsbDevice.getInterfaceCount()) {
				connection1 = mUsbDevice.getInterface(connection);
				mInterface = connection1;
			}

			if (mInterface != null) {
				connection1 = null;
				/* 判断当前USB设备是否有权限 */
				if (manager.hasPermission(mUsbDevice)) {
					UsbDeviceConnection connection11 = manager.openDevice(mUsbDevice);
					if (connection11 == null) {
						return -99;
					} else {
						if (connection11.claimInterface(mInterface, true)) {
							this.mDeviceConnection = connection11;
							this.getEndpoint(this.mDeviceConnection, mInterface);
						} else {
							connection11.close();
						}

						return 1;
					}
				} else {
					(new Thread() {
						public void run() {
							Context var10000 = HsOTGSDK.this.instance;
							PendingIntent pi = PendingIntent.getBroadcast(var10000, 0,
									new Intent("com.android.USB_PERMISSION"), 0);
							manager.requestPermission(mUsbDevice, pi);
						}
					}).start();
					return -1;
				}
			} else {
				return -99;
			}
		}
	}

	private void getEndpoint(UsbDeviceConnection mDeviceConnection, UsbInterface mInterface) {
		if (mInterface.getEndpoint(1) != null) {
			this.epOut = mInterface.getEndpoint(1);
		}

		if (mInterface.getEndpoint(0) != null) {
			this.epIn = mInterface.getEndpoint(0);
		}

	}

	public void usbSendRecv(byte[] send, byte[] recv, long sendTime, long recvTime, int ret) {
		this.mDeviceConnection.bulkTransfer(this.epOut, send, send.length, (int) sendTime);
		this.mDeviceConnection.bulkTransfer(this.epIn, recv, recv.length, (int) recvTime);
	}
}
